<script>
	export default {
		// 只有 app 才会有 onLaunch 的生命周期
		onLaunch: function() {
			console.log('App Launch')
			// this.getRenewData()

			// #ifdef APP-PLUS || APP-PLUS-NVUE
			this.checkVersion()
			// #endif
		},
		// 捕获 app error
		onError (err) {
		   console.log(err);
		},
		onShow: function() {
			// console.log('App Show');
			// #ifndef MP-WEIXIN
			// 非微信小程序
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			let curRoute = routes.length ? routes[routes.length - 1].route : ''; //获取当前页面路由
			if (curRoute === 'pages/account/login' || curRoute === 'pages/account/forget-psw') {
				// 登录页面
				return false;
			}
			// 获取用户信息
			uni.getStorage({
				key: 'token',
				success: function (res) {
					console.log(res, 'res');
					// 已存储userInfo(登陆)，页面跳至“首页”
					// uni.reLaunch({
					// 	url: '/pages/index/index'
					// });
				},
				fail: function (err) {
					// // 未存储userInfo(登陆)，页面跳至“登陆页”
					// uni.reLaunch({
					// 	url: '/pages/account/login'
					// });
				}
			});
			// #endif
		},
		onHide: function() {
			// console.log('App Hide');
		},
		methods: {
			// #ifdef APP-PLUS || APP-PLUS-NVUE
			// app更新检测
			checkVersion() {
				// 获取应用版本号
				let version = plus.runtime.version;

				//检测当前平台，如果是安卓则启动安卓更新
				uni.getSystemInfo({
					success: res => {
						this.updateHandler(res.platform, version);
					}
				})
			},
			// 更新操作
			updateHandler(platform, version) {
				let data = {
					platform: platform,
					version: version
				}
				let _this = this;
				
				this.$Requset({
					url: this.$Api['home']['checkVersion'],
					data: data,
					method: 'post',
					success: (res) => {
						if (res.status && res.data[0].version) {
							const info = res.data[0];
							if (info.version !== '' && info.version > version) {
								uni.showModal({
									//提醒用户更新
									title: '更新提示',
									content: info.note,
									success: res => {
										if (res.confirm) {
											plus.runtime.openURL(info.download_url)
										}
									}
								})
							}
						}
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			// #endif
			// APP 是否强制更新
			getRenewData() {
				this.$Requset({
					url: this.$Api['home']['classify'],
					data: {},
					success: (res) => {
						const { data, status } = res;
						if (!status) {
							return;
						}
						if (data) {
							this.forceRenewApp();
						}
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			// 强制更新APP弹窗
			forceRenewApp() {
				console.log('forceRenewApp')
				uni.showModal({
					title: '发现新版本',
					content: '确认下载更新',
					showCancel: false, // 隐藏取消按钮
					success: function (res) {
						return false
						// 确认按钮
						if (res.confirm) {
							return false
							// callback && callback();
						}
					}
				});
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	page{
		background-color: #f5f5f5;
	}
	.home {
		position: relative;
	}
	/* #ifdef H5 */
	.uni-body {
		display: block;
		max-width: 400px!important;
		margin: 0 auto;
	}
	.pages-index-index {
		display: block;
		max-width: 400px!important;
		margin: 0 auto;
	}
	.uni-tabbar-bottom {
		max-width: 400px!important;
		left: 50%!important;
		margin-left: -200px;
	}
	.uni-tabbar {
		max-width: 400px!important;
		left: 50%!important;
		margin-left: -200px;
	}
	.header-main {
		display: block;
		max-width: 400px!important;
		left: 50%!important;
		margin-left: -200px;
	}
	/* #endif */
	.block{
		box-sizing: border-box;
		background-color: #fff;
		margin-bottom: 18rpx;
		padding: 32rpx 36rpx;
		border-radius: 22rpx;
		/* &:nth-last-child(1) {
			border-bottom-left-radius: 0;
			border-bottom-right-radius: 0;
			margin-bottom: 0;
		} */
	}
	/* 1行超出省略 */
	.ellipsis-1{
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	/* 2行超出省略 */
	.ellipsis-2{
		word-break: break-all;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2; /* 这里是超出几行省略 */
		overflow: hidden;
	}
	/* 3行超出省略 */
	.ellipsis-3{
		word-break: break-all;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3; /* 这里是超出几行省略 */
		overflow: hidden;
	}
</style>
